package com.hss.recyclerviewtest.section;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import com.hss.recyclerviewtest.R;
import com.hss.recyclerviewtest.itemtouch.MyAdapter;
import com.hss.recyclerviewtest.itemtouch.SimpleItemTouchHelperCallback;
import com.oushangfeng.pinnedsectionitemdecoration.PinnedHeaderItemDecoration;
import com.oushangfeng.pinnedsectionitemdecoration.callback.OnHeaderClickAdapter;

import java.util.ArrayList;
import java.util.List;

public class SectionActivity extends AppCompatActivity {
    RecyclerView rcv;
    StockAdapter adapter;
    private PinnedHeaderItemDecoration mHeaderItemDecoration;

    final  String  TAG = SectionActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        rcv = findViewById(R.id.rcv);
//        rcv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        rcv.setLayoutManager(new GridLayoutManager(this, 2));



        OnHeaderClickAdapter clickAdapter = new OnHeaderClickAdapter() {

            @Override
            public void onHeaderClick(View view, int id, int position) {
                Log.e(TAG, "onHeaderClick");
                switch (id) {
                    case R.id.fl:
                        // case OnItemTouchListener.HEADER_ID:
                        Toast.makeText(SectionActivity.this, "click, tag: " + adapter.getData().get(position).pinnedHeaderName, Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_more:
                        Toast.makeText(SectionActivity.this, "click " + adapter.getData().get(position).pinnedHeaderName + "'s more button", Toast.LENGTH_SHORT)
                                .show();
                        break;
                    case R.id.checkbox:
                        final CheckBox checkBox = (CheckBox) view;
                        checkBox.setChecked(!checkBox.isChecked());
                        // 刷新ItemDecorations，导致重绘刷新头部
                        rcv.invalidateItemDecorations();

                        adapter.getData().get(position).check = checkBox.isChecked();
                        adapter.notifyItemChanged(position + mHeaderItemDecoration.getDataPositionOffset());

                        break;
                }
            }

        };
        mHeaderItemDecoration = new PinnedHeaderItemDecoration.Builder(StockEntity.StockInfo.TYPE_HEADER)
                .setDividerId(R.drawable.divider).enableDivider(true)
                .setClickIds(R.id.iv_more, R.id.fl, R.id.checkbox)
                .disableHeaderClick(false).setHeaderClickListener(clickAdapter).create();
        rcv.addItemDecoration(mHeaderItemDecoration);


        List<StockEntity.StockInfo> data = new ArrayList<>();

        data.add(new StockEntity.StockInfo(StockEntity.StockInfo.TYPE_HEADER, "涨幅榜"));

        for (int i=0;i<20;i++){
            StockEntity.StockInfo info = new StockEntity.StockInfo(StockEntity.StockInfo.TYPE_DATA);
            info.stock_name= "aaa"+i;
            data.add(info);
        }

        data.add(new StockEntity.StockInfo(StockEntity.StockInfo.TYPE_HEADER, "跌幅榜"));
        for (int i=0;i<20;i++){
            StockEntity.StockInfo info = new StockEntity.StockInfo(StockEntity.StockInfo.TYPE_DATA);
            info.stock_name= "bbb"+i;
            data.add(info);
        }

        data.add(new StockEntity.StockInfo(StockEntity.StockInfo.TYPE_HEADER, "换手率"));
        for (int i=0;i<20;i++){
            StockEntity.StockInfo info = new StockEntity.StockInfo(StockEntity.StockInfo.TYPE_DATA);
            info.stock_name= "ccc"+i;
            data.add(info);
        }

         adapter = new StockAdapter(data);
        rcv.setAdapter(adapter);

//        adapter.addHeaderView(LayoutInflater.from(this).inflate(R.layout.item_data, rcv, false));
        // 因为添加了1个头部，他是不在clickAdapter.getData这个数据里面的，所以这里要设置数据的偏移值告知ItemDecoration真正的数据索引
//        mHeaderItemDecoration.setDataPositionOffset(mAdapter.getHeaderLayoutCount());


//        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter);
//        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
//        touchHelper.attachToRecyclerView(rcv);
    }
}
